import { resolve } from 'path'
import type { UserConfig } from 'vite'
import AntdDayjsWebpackPlugin from 'antd-dayjs-webpack-plugin'

const fs = require('fs')
const dotenv = require('dotenv')
const envFiles = [
  /** default file */ `.env`,
  /** mode file */ `.env.${process.env.NODE_ENV}`
]
for (const file of envFiles) {
  const envConfig = dotenv.parse(fs.readFileSync(file))
  for (const k in envConfig) {
    process.env[k] = envConfig[k]
  }
}

function pathResolve(dir: string) {
  return resolve(__dirname, '.', dir)
}

const viteConfig: UserConfig = {
  port: 8000,
  // 是否自动在浏览器打开
  open: false,

  // 服务端渲染
  ssr: false,

  alias: {
    '/@/': pathResolve('./src'),
  },

  /**
   * 资源输出路径
   * @default '_assets'
   */
  assetsDir: 'assets',

  base: process.env.BASE_URL,
  /**
   * Directory relative from `root` where build output will be placed. If the
   * directory exists, it will be removed before the build.
   * @default 'dist'
   */
  outDir: process.env.VITE_OUTPUT_DIR,

  // 会使用 rollup 对 包重新编译，将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
  optimizeDeps: {
    include: ['@ant-design/icons-vue'],
  },

  plugins: [
    new AntdDayjsWebpackPlugin(),
  ]

}

export default viteConfig
